#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>

#define maxn 30
#define maxm 40

#define max(x,y) ((x>y)?x:y)
#define min(x,y) ((x<y)?x:y)

#define DEATH -100000
//#define MYDEBUG

int map[maxn][maxm];
const int ME = 8103;
const int BODY = 2018000103;

void get_map() 
{
    for(int i=0; i<maxn; i++) 
    {
        for (int j=0; j<maxm; j++) 
    	{
            scanf("%d", &map[i][j]);
    	}
    }
}

int locate_my_pos(int* x, int* y)
{
    int success = 0;

    for(int i=0; i<maxn; i++) 
    {
        for (int j=0; j<maxm; j++) 
        {
            if (ME == map[i][j]) 
            {
                *x = j;
                *y = i;
                success = 1;
                break;
            }
        }
    }
    return success;
}

int is_other_snake_body(const int x, const int y)
{
    return (map[y][x]!=ME && map[y][x] != BODY && map[y][x]>10000);
}

int is_other_snake_head(const int x, const int y)
{
    return (map[y][x]!=ME && map[y][x] != BODY && map[y][x]>2000 && map[y][x]<10000);
}

int is_other_snake(const int x, const int y)
{
    return is_other_snake_body(x,y) || is_other_snake_head(x,y);
}

int is_myself(const int x, const int y)
{
    return (map[y][x] == BODY || map[y][x]==ME);
}


int out_boundary(const int x, const int y)
{
    const int offset = 1;
    return (x<(offset-1)) || (x>(maxm-offset)) || (y<(offset-1)) || (y>(maxn-offset));
}

void next_pos(const int x, const int y, const int action, int* newx, int* newy)
{
   
    if (0 == action) { *newx = x-1; *newy = y; }
    else if (1 == action) { *newx = x; *newy = y-1; }
    else if (2 == action) { *newx = x+1; *newy = y; }
    else { *newx = x; *newy = y+1; }
}

int estimate_current_direction(const int x, const int y)
{
    if ( x<(maxm-1) && BODY==map[y][x+1])
        return 0; // the snake is going left
    else if ( x>1 && BODY == map[y][x-1])
        return 2; // the snake is going right
    else if ( y<(maxn-1) && BODY==map[y+1][x])
        return 1; // the snake is going up
    else
        return 3; // the snake is going down
}

int estimate_long_term_award(const int x, const int y, const int action)
{
    const int radius = 5;
    int topx=0, topy=0, botx=0, boty=0;
    // compute bounding box based on the action
    switch (action) {
       case 0: // look at the left side
           topx = max(0, x-2*radius);
           topy = max(0, y-radius);
           botx = max(0, x-1);
           boty = min(maxn-1, y+radius);
           break;

       case 1: // look at the top side
           topx = max(0, x-radius);
           topy = max(0, y-2*radius);
           botx = min(maxm-1, x+radius);
           boty = max(0, y-1);
           break;
   
       case 2: // look at the right side
           topx = min(maxm-1, x+1);
           topy = max(0, y-radius);
           botx = min(maxm-1, x+2*radius);
           boty = min(maxn-1, y+radius);
           break;

       case 3: // look down
           topx = max(0, x-radius);
           topy = min(maxn-1, y+1);
           botx = min(maxm-1, x+radius);
           boty = min(maxn-1, y+2*radius);
           break;
    }
    #ifdef MYDEBUG
    printf("x=%d, y=%d, action=%d ->, topx=%d, topy=%d, botx=%d, boty=%d\n", x, y, action, topx, topy, botx, boty);
    #endif

    double award = 0.0;

    for (int i=topy; i<=boty; i++) {
        for (int j=topx; j<=botx; j++) {
            double this_part = 0;
            double weight = 1.0/(abs(j-x) + abs(i-y) + 1.0);
            if (is_other_snake_body(j, i)) {
                #ifdef MYDEBUG
                printf("found other snake at [%d,%d]=%d\n", i, j, map[i][j]);
                #endif
                this_part = -1.5;
            } 
            else if (is_other_snake_head(j,i)) {
                this_part = -50;
            }
            else if (is_myself(j,i)) {
                this_part = -5;
            }
            else {
                this_part = map[i][j]*map[i][j];
            }
            award += weight * this_part;
        }
    }
    return award;
}

double estimate_award(const int x, const int y, const int action)
{
    int current_direction = estimate_current_direction(x, y);
    int diff = action - current_direction;
    if ( 2==diff || -2==diff) {
        #ifdef MYDEBUG
            printf("cannot reverse the direction\n");
        #endif
        return DEATH; // cannot reverse the direction
    }

    int newx=0, newy=0;
    next_pos(x, y, action, &newx, &newy);
    #ifdef MYDEBUG
        printf("[%d][%d], action %d -> [%d][%d]\n", y, x, action, newy, newx);
    #endif

    if (out_boundary(newx, newy) || is_other_snake(newx, newy) || is_myself(newx, newy) ) {
       return DEATH;
    }

    double short_award = is_myself(newx, newy) ? 0 : -map[newy][newx];
    double long_award = estimate_long_term_award(x, y, action);
    #ifdef MYDEBUG
    printf("short award %f, long award %f\n", short_award, long_award);
    #endif
    return 0.95*short_award + 0.05*long_award;
    //return -map[newy][newx]; // get food?
}

void solve() {
   int x = 0, y = 0;
   int found = locate_my_pos(&x, &y);
   assert(1 == found);
   int current_direction = estimate_current_direction(x, y);
   #ifdef MYDEBUG
       printf("x=%d, y=%d, direction=%d\n", x, y, current_direction);
   #endif
   srand(time(NULL));
    
   int best_action = 0;
   double max_val = 2*DEATH;
   int action = rand()%4;

   for (int i=0; i<4; i++)
   {
       double val = estimate_award(x, y, action); //evaluate(x, y, current_direction, action);
       #ifdef MYDEBUG
           printf("action %d -> award %f\n", action, val);
       #endif
       if (max_val < val) {
           max_val = val;
           best_action = action;
       }
       action = (action + 1)%4;
   }
   printf("%d", best_action);
}

int main()
{
   #ifndef MYDEBUG
       get_map();
   #endif

   #ifdef MYDEBUG
       for (int i=0; i<maxn; i++) 
          for (int j=0; j<maxm; j++)
              map[i][j] = 0;
       //map[maxn/2][0] = ME;
       //map[maxn/2][1] = BODY;
       //map[maxn/2][maxm-1] = ME;
       //map[maxn/2][maxm-2] = BODY;       

       //top-right corner
       //map[0][maxm-1] = ME;
       //map[0][maxm-2] = BODY;
       //map[0][maxm-1] = ME;
       //map[1][maxm-1] = BODY;

       //bottom-right corner
       //map[maxn-1][maxm-1] = ME;
       //map[maxn-2][maxm-1] = BODY;
       //map[maxn-2][maxm-1] = ME;
       //map[maxn-1][maxm-1] = BODY;

       //bottom-left corner
       //map[maxn-1][0] = ME;  //horizontal snake
       //map[maxn-1][1] = BODY;
       map[maxn-1][0] = ME;  // going down
       map[maxn-2][0] = BODY;       

   #endif
   solve();
   return 0;
}
